import 'dart:convert';

import 'package:amway_superapp_creator_plugin/models/create_user_info_model.dart';
import 'package:amway_superapp_creator_plugin/net/http_manager.dart';
import 'package:amway_superapp_creator_plugin/utils/sp_util.dart';
import 'package:amway_superapp_creator_plugin/viewmodels/base_model.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../api.dart';
import '../view_state.dart';

class CreateLaunchViewModel extends BaseModel {
  CreateLaunchViewModel({@required Api api}) : super(api: api);
  CreateUserInfoModel _createUserInfoModel;
  bool isDraft = false;
  String _shareLink;


  void getUserSvaedraftData(String amwayAda) {
    SpUtil.getData("${SpUtil.user_data}$amwayAda").then((value) {
      Map<String, dynamic> response = json.decode(value);
      var createUserInfoModel = CreateUserInfoModel.fromJson(response);
      if (null != createUserInfoModel && null != createUserInfoModel.nick) {
        isDraft = true;
      } else {
        isDraft = false;
      }
      notifyListeners();
    });
  }

  void saveInitParmer(String amwayAda,String amwayId,String token) async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setString("Amway-Ada", amwayAda);
    prefs.setString("Amway-Id", amwayId);
    prefs.setString("token", token);
    


  }

  void getUserInfo(String amwayAda, {bool isVisible = true}) async {
    setState(ViewState.Busy);
    HttpManager().get(
        url: "/apis/v1/creator/personal-configs/$amwayAda",
        tag: null,
        successCallback: (data) {
          setState(ViewState.Idle);
          if (data != null) {
            createUserInfoModel = CreateUserInfoModel.fromJson(data);
          } else {
            getUserSvaedraftData(amwayAda);
          }
        },
        errorCallback: (data) {
          setState(ViewState.Idle);
          createUserInfoModel = null;
        });
  }

  String get shareLink => _shareLink;

  set shareLink(String value) {
    _shareLink = value;
    notifyListeners();
  }
  void queryShareLink({String amwayAda,String type='h5'}){
    HttpManager().get(
        url: "/apis/v1/creator/index/getStationUrl/${amwayAda}/${type}",
        tag: null,
        successCallback: (data) {
          shareLink=data;


        },
        errorCallback: (data) {
          print(data);

        });
  }
  CreateUserInfoModel get createUserInfoModel => _createUserInfoModel;
  bool isOpenCreate = false;

  set createUserInfoModel(CreateUserInfoModel value) {
    _createUserInfoModel = value;

    if (createUserInfoModel != null &&
        createUserInfoModel.nick != null &&
        createUserInfoModel.nick.length > 0) {
      List<DisplayAuditsBean> displayAudits = createUserInfoModel.displayAudits;
      if (displayAudits != null && displayAudits.length > 0) {
        displayAudits.forEach((element) {
          if (element.data.isNotEmpty &&
              element.data == createUserInfoModel.nick) {
            if (element.status == "UN_PASS") {
              isOpenCreate = false;
            } else {
              isOpenCreate = true;
            }
          }
        });
      }
    }

    notifyListeners();
  }
}
